package com.example.hm_dtfkz.ui.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
import android.util.TypedValue;
import android.view.View;
import android.view.WindowManager;

import com.android.base.util.ActivityClickUtil;
import com.android.base.util.DisplayUtil;
import com.android.base.util.SystemUtil;
import com.dq.ad.AdManager;
import com.example.hm_dtfkz.MainActivity;
import com.example.hm_dtfkz.R;
import com.example.hm_dtfkz.config.AppConfig;
import com.example.hm_dtfkz.databinding.DialogWithErrorBinding;

import java.util.Locale;

/**
 * FileName: DialogWithError
 * Author: admin
 * Date: 2022/3/14 10:22
 * Description:
 */
public class DialogWithError extends Dialog implements View.OnClickListener {

    private DialogWithErrorBinding errorBinding;
    private final Activity activity;
    public static int TYPE_NORMAL = 0;
    public static int TYPE_QUEUE = 1;
    public static int TYPE_NUM = 2;//提现剩余看视频次数
    public static int TYPE_TIME = 3;//提现等待倒计时
    public static int TYPE_TIP = 4;//单行文字tip，文字内容是tipStr
    private int dialogType = TYPE_NORMAL;
    private int num = 0;
    private String tipStr = "";
    long leftTime;

    public DialogWithError(Activity a) {
        super(a, R.style.dialog_loading);
        this.activity = a;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        errorBinding = DialogWithErrorBinding.inflate(activity.getLayoutInflater());
        setContentView(errorBinding.getRoot());

        getWindow().getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = DisplayUtil.getScreenWidth(activity);
        lp.height = DisplayUtil.getScreenHeight(activity);
        getWindow().setAttributes(lp);
        getWindow().setWindowAnimations(R.style.dialog_answer_correct);

        setCancelable(false);
        setCanceledOnTouchOutside(false);

        initView();
        initLister();
    }

    private void initView() {
        String content1;
        String content2;
        String content3;
        if (dialogType == TYPE_QUEUE) {
            content1 = "已有还在审核中的提现申请";
            content2 = "请稍后重试";
            content3 = "若超过半小时还未审核成功，请联系客服";
            errorBinding.imgMakeMoney.setImageResource(R.drawable.icon_dialog_with_error_contact_us);
            errorBinding.tvContent1.setText(content1);
            errorBinding.tvContentTime.setVisibility(View.GONE);
        } else if (dialogType == TYPE_NUM) {
            if (this.num == 0) {
                content1 = "";
                content2 = "可立即提现";
                content3 = "";
                errorBinding.tvContent3.setTextSize(TypedValue.COMPLEX_UNIT_SP, 8);
                errorBinding.imgMakeMoney.setImageResource(R.drawable.icon_dialog_with_error_ok);
            } else {
                content1 = "再看<font color='#E74C42'>" + this.num + "</font>个激励视频即可";
                content2 = "提现到账";
                content3 = "";
                errorBinding.tvContent2.setTextColor(Color.parseColor("#E74C42"));
                errorBinding.imgMakeMoney.setImageResource(R.drawable.icon_dialog_with_error_go);
            }

            errorBinding.tvTitle.setText("提现助手");
            errorBinding.tvContent1.setTextColor(activity.getResources().getColor(R.color.Color_000000));
            errorBinding.tvContent1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
            errorBinding.tvContent2.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
            errorBinding.tvContent1.setTypeface(Typeface.DEFAULT_BOLD);
            errorBinding.tvContent2.setTypeface(Typeface.DEFAULT_BOLD);
            errorBinding.tvContent1.setText(Html.fromHtml(content1));
            errorBinding.tvContentTime.setVisibility(View.GONE);
        } else if (dialogType == TYPE_TIP) {
            content1 = "";
            content2 = this.tipStr;
            content3 = "";
            errorBinding.imgMakeMoney.setImageResource(R.drawable.icon_dialog_with_error_ok);
            errorBinding.tvTitle.setText("提现助手");
            errorBinding.tvContent1.setText(content1);
            errorBinding.tvContent1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 8);
            errorBinding.tvContent2.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
            errorBinding.tvContent2.setTypeface(Typeface.DEFAULT_BOLD);
            errorBinding.tvContent1.setText(content1);
            errorBinding.tvContentTime.setVisibility(View.GONE);
        } else if (dialogType == TYPE_TIME) {
            content1 = "今日已提现多次，";
            content2 = "离下次提现倒计时";
            content3 = "";
            errorBinding.imgMakeMoney.setImageResource(R.drawable.icon_dialog_with_error_make_money);
            errorBinding.tvTitle.setText("提现助手");
            errorBinding.tvContent1.setTextColor(activity.getResources().getColor(R.color.Color_000000));
            errorBinding.tvContent2.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
            errorBinding.tvContent1.setText(content1);
            leftTime = this.num;//86400 - (System.currentTimeMillis() / 1000 + 8 * 3600) % 86400;
            errorBinding.tvContentTime.setText(String.format(Locale.CHINA, "%02d:%02d:%02d", leftTime / 3600, (leftTime % 3600) / 60, (leftTime % 3600) % 60));
            errorBinding.tvContent3.setVisibility(View.GONE);
            Handler mHandler = new Handler();
            Runnable r = new Runnable() {
                @Override
                public void run() {
                    if (activity.isFinishing()) {
                        return;
                    }
                    leftTime--;
                    errorBinding.tvContentTime.setText(String.format(Locale.CHINA, "%02d:%02d:%02d", leftTime / 3600, (leftTime % 3600) / 60, (leftTime % 3600) % 60));
                    //每隔1s循环执行run方法
                    mHandler.postDelayed(this, 1000);
                }
            };
            mHandler.postDelayed(r, 1000);
        } else {
            content1 = "提现审核中";
            content2 = "请联系客服处理";
            content3 = "客服QQ群：" + AppConfig.QQ_GROUP;
            errorBinding.imgMakeMoney.setImageResource(R.drawable.icon_dialog_with_error_ok);
            errorBinding.tvContent1.setText(content1);
            errorBinding.tvContentTime.setVisibility(View.GONE);
        }

        errorBinding.tvContent2.setText(content2);
        errorBinding.tvContent3.setText(content3);

        AdManager.getInstance().showNativeAD(activity, errorBinding.layoutNative, DisplayUtil.getScreenWidthDp(activity) - 48, 0, AdManager.NATIVE_AD_TYPE_UP_IMAGE, false, new AdManager.OnNativeVideoADListener() {
            @Override
            public void onSuccess() {
                errorBinding.layoutNative.setBackgroundColor(activity.getResources().getColor(R.color.Color_FFFFFF));
            }

            @Override
            public void onClosed() {
                errorBinding.layoutNative.setBackgroundColor(activity.getResources().getColor(android.R.color.transparent));
            }

            @Override
            public void onError() {

            }

            @Override
            public void onClick() {

            }

            @Override
            public void onShow(View view) {

            }
        });
    }

    public DialogWithError setType(int type) {
        this.dialogType = type;
        return this;
    }

    public DialogWithError setNum(int num) {
        this.num = num;
        return this;
    }

    public DialogWithError setTipStr(String tip) {
        this.tipStr = tip;
        return this;
    }

    private void initLister() {
        errorBinding.imgClose.setOnClickListener(this);
        errorBinding.imgMakeMoney.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (ActivityClickUtil.getInstance().isFastClick()) {
            //防止用户快速点击
            return;
        }
        dismiss();
        if (v.getId() == R.id.imgMakeMoney) {
            if (dialogType == TYPE_QUEUE) {
                SystemUtil.joinQQGroup(activity, AppConfig.QQ_GROUP_KEY);
            } else if (dialogType == TYPE_NUM && this.num > 0) {
                if ((activity instanceof MainActivity)) {
                    ((MainActivity) activity).showHome();
                }
            }
        }
    }
}
